package com.kun.service_member.controller;


import com.kun.service_member.entity.UcenterMember;
import com.kun.service_member.entity.vo.RegisterVo;
import com.kun.service_member.service.UcenterMemberService;
import com.kun.utils.JwtUtils;
import com.kun.utils.R;
import com.kun.vo.UcenterMemberOrder;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-10-25
 */
@RestController
@RequestMapping("/service_member/ucenter-member")
@CrossOrigin
public class UcenterMemberController {
    @Autowired
    private UcenterMemberService memberService;
    @PostMapping("login")
    public R login(@RequestBody UcenterMember ucenterMember){
        System.out.println(ucenterMember);
        String token= memberService.login(ucenterMember);
        return R.ok().data("token",token);
    }
    @PostMapping("register")
    public R registerUser(@RequestBody RegisterVo  registerVo){
        memberService.register(registerVo);
        return R.ok();
    }
    //根据token获取id及用户信息
    @GetMapping("getUserInfo")
    public R getUserInfo(HttpServletRequest request){
        String id = JwtUtils.getMemberIdByJwtToken(request);
        UcenterMember member = memberService.getById(id);
        return R.ok().data("userInfo",member);

    }
    @PostMapping("getUserInfoById/{id}")
    public UcenterMemberOrder getMemberOrder(@PathVariable String id){
        UcenterMember ucenterMember = memberService.getById(id);
        UcenterMemberOrder memberOrderVo = new UcenterMemberOrder();
        BeanUtils.copyProperties(ucenterMember,memberOrderVo);
        return memberOrderVo;
    }

    //查询某一天的注册人数
    @GetMapping("countRegister/{day}")
    public R countRegister(@PathVariable String day ){
        int i= memberService.countRegisterDay(day);
        return R.ok().data("countRegister",i);
    }

}

